// window.onload = function () {
// var text = document.getElementById("text");
var _id = window.localStorage.getItem('_id');
console.log(_id);
$("#usersec table tr:even").css("background", "rgba(0,0,0,0.1)");
//获取用户信息，并填入表格
var uus = document.getElementById('uus');
// var u_id = document.getElementById('u_id');
var uphone = document.getElementById('uphone');
var uage = document.getElementById('uage');
var usex = document.getElementById('usex');
//
var cus = document.getElementById('cus');
// var c_id = document.getElementById('c_id');
var cphone = document.getElementById('cphone');
var cage = document.getElementById('cage');
var csex = document.getElementById('csex');
$.ajax({
    type: "post",
    url: "http://118.195.129.130:3000/user/inquire",
    async: false,
    data: {
        _id: _id
    },
    success: function (data) {
        uus.innerHTML = data.data[0].us;
        uphone.innerHTML = data.data[0].phone;
        uage.innerHTML = data.data[0].age;
        var temp = data.data[0].sex;
        if (temp == 0) {
            usex.innerHTML = '男';
        } else {
            usex.innerHTML = '女';
        }
        console.log(data);
        console.log(temp);
    },
    error: function (err) {
        console.log(err);
    }
})

//点击修改，出现阴影
$("#usersec div a").click(function () {
    $("#shadowbox").css("display", "block");
        cus.value = uus.innerHTML;
        cphone.value = uphone.innerHTML;

        cage.value = uage.innerHTML;
        csex.value =  usex.innerHTML ;
})

// 修改用户信息；并进行上传
$("#shadowbox button").eq(1).click(function () {
    $("#shadowbox").css("display", "none");
})

function transMath(sex) {
    console.log(sex);
    if (sex == '男') {
        console.log('1');
        return 0;
    } else {
        return 1;
    }
}
function transSex(math) {
    if (math == 0) {
        return "男";
    } else {
        return "女";
    }
}

$("#shadowbox button").eq(0).click(function () {

    if ($(".cus").val() != '') {
        var flag = confirm('确定要进行修改吗?');
        if (flag == true) {
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/user/mod",
                async: false,
                data: {
                    us: cus.value,
                    _id: _id,
                    phone: cphone.value,
                    age: Number(cage.value),
                    sex: Number(transMath(csex.value))
                },
                success: function (data) {

                    if (data.err != '-1' && cus.value != '') {
                        $.ajax({
                            type: "post",
                            url: "http://118.195.129.130:3000/user/inquire",
                            async: false,
                            data: {
                                _id: _id
                            },
                            success: function (data) {
                                console.log(data);//修改后的信息
                                uus.innerHTML = data.data[0].us;
                                uphone.innerHTML = data.data[0].phone;
                                uage.innerHTML = data.data[0].age;
                                usex.innerHTML = transSex(Number(data.data[0].sex));
                                console.log(Number(data.data[0].sex));
                            },
                            error: function (err) {
                                console.log(err);
                            }
                        })
                        // console.log($(".us").val());
                    } else {
                        $("#shadowbox").css("display", "none");
                        alert("修改失败");
                    }
                    $("#shadowbox").css("display", "none");
                    console.log(data);
                },
                error: function (err) {
                    console.log(err);
                }
            })
        } else {
            alert('修改失败!');
        }
    } else {
        alert('用户名和id不能为空！');
    }
})

var exit = document.querySelector('#exit');
exit.addEventListener('click', function () {
    var flag = confirm("确认退出吗？");
    if (flag == true) {
        window.location.replace('loading.html');
        $.ajax({
            type: "post",
            url: "http://118.195.129.130:3000/user/out",
            async: false,
            data: {
                Null: ""
            },
            success: function (data) {
                window.location.replace('loading.html');
                console.log(data);
            },
            error: function (err) {
                console.log(err);
            }
        })

    }
})

// var lis = document.querySelectorAll('#tab li');
// var secs = document.querySelectorAll('section');
var that;
class Tab {
    constructor(id) {
        that = this;
        this.main = document.querySelector(id);
        this.lis = this.main.querySelectorAll('li');
        this.secs = document.querySelectorAll('section');
        this.init();

    }
    init() {
        this.lis[0].style.background = '#629feb';
        this.secs[0].style.display = 'block';

        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].setAttribute('index', i);
            this.lis[i].onclick = this.toggleTab;
        }
    }
    //    1.切换功能;
    toggleTab() {
        for (var i = 0; i < that.lis.length; i++) {
            that.lis[i].style.background = 'rgb(49,46,46)';
            that.secs[i].style.display = 'none';
            // $( that.secs[i]).hide();
        }
        this.style.background = '#629feb';
        that.secs[this.getAttribute('index')].style.display = 'block';
        // $( that.secs[this.getAttribute('index')]).show();
    }
}
new Tab('#tab');
// var per_changes = document.querySelectorAll('.per_change');
$("#usersmsg li tr").even().css("background", "skyblue");
var pages;
$.ajax({
    type: "get",
    url: "http://118.195.129.130:3000/users/allpage_users",
    async: false,
    data: {
        Null: ""
    },
    success: function (data) {
        pages = data.pages;
        console.log(pages);

    }
})

var usersmsg = document.getElementById('usersmsg');
for (var i = 1; i < Math.ceil(pages / 5); i++) {
    var li = usersmsg.children[0].cloneNode(true);
    // li.setAttribute('index', i)
    usersmsg.appendChild(li);
}
var usersmsg_lis = usersmsg.querySelectorAll('li');
usersmsg_lis[0].setAttribute('index', 0);
for (var i = 0; i < usersmsg_lis.length; i++) {
    $("#usersmsg li").eq(i).hide();
}
$("#usersmsg li").eq(0).show();
// 将用户信息填入表格中
var usersMsg;
var arrId = [];
var per_changes = document.querySelectorAll('.per_change')
$.ajax({
    type: "post",
    url: "http://118.195.129.130:3000/users/getInfoByPage_users",
    async: false,
    data: {
        page: 1,
        per_page: pages
    },
    success: function (data) {
        usersMsg = data.data;
        console.log(usersMsg);
        // var per_changes = document.querySelectorAll('.per_change');
        var trs = document.querySelectorAll('#usersmsg tr');
        console.log(trs.length);
        for (var i = 0; i < usersMsg.length; i++) {
            trs[i].children[1].innerHTML = usersMsg[i].us;
            trs[i].children[2].innerHTML = usersMsg[i].phone;
            trs[i].children[3].innerHTML = usersMsg[i].age;
            arrId.push(usersMsg[i]._id);
            if (usersMsg[i].sex == 0) {
                trs[i].children[4].innerHTML = '男';
            } else {
                trs[i].children[4].innerHTML = '女';
            }
        }
    },
    error: function (err) {
        console.log(err);
    }
})
//获取编辑框 内容
var trs = document.querySelectorAll('#usersmsg tr');
console.log(trs.length);
var flag = 0;
var num = 0;
var that;
var eid_cus = document.getElementById('eid_cus');
var eid_cage = document.getElementById('eid_cage');
var eid_csex = document.getElementById('eid_csex');
for (var i = 0; i < per_changes.length; i++) {
    per_changes[i].setAttribute("index", i);
    per_changes[i].addEventListener('click', function () {
        num = this.getAttribute("index");
        that = this;
        eid_cus.value = trs[num].children[1].innerHTML ;
        
         eid_cage.value = trs[num].children[3].innerHTML;
        eid_csex.value = trs[num].children[4].innerHTML;
        $("#eidshadowbox").css("display", "block");
        $("#eidshadowbox button").eq(0).click(function () {
            if ($(".eid_cus").val() != '') {
                flag = confirm('确定要进行修改吗?');
                if (flag == true) {
                    $.ajax({
                        type: "post",
                        url: "http://118.195.129.130:3000/users/update_users",
                        async: false,
                        data: {
                            us: eid_cus.value,
                            _id: arrId[num],
                            age: Number(eid_cage.value),
                            sex: Number(transMath(eid_csex.value))
                        },
                        success: function (data) {
                            console.log(data);
                            if (data.msg != "用户名已存在") {
                                $.ajax({
                                    type: "post",
                                    url: "http://118.195.129.130:3000/user/inquire",
                                    async: false,
                                    data: {
                                        _id: arrId[num]
                                    },
                                    success: function (data) {
                                        console.log(data);
                                        console.log(arrId[num]);
                                        console.log(num);
                                        trs[num].children[1].innerHTML = data.data[0].us;
                                        trs[num].children[2].innerHTML = data.data[0].phone;
                                        trs[num].children[3].innerHTML = data.data[0].age;
                                        trs[num].children[4].innerHTML = transSex(Number(data.data[0].sex));
                                        // that.parentNode.parentNode.children[]
                                        $("#eidshadowbox").css("display", "none")
                                    }

                                })
                            } else {
                                alert("用户名已存在")
                            }
                        },
                        error: function (err) {
                            console.log(err);
                        }
                    })
                } else {
                    alert('修改失败!');
                }
            }
            else {
                alert('用户名不能为空！');
            }
        })
    })
}
$("#eidshadowbox button").eq(1).click(function () {
    $("#eidshadowbox").css("display", "none");
})
$("#eidshadowbox i").click(function () {
    $("#eidshadowbox").css("display", "none");
})
$("#shadowbox i").click(function () {
    $("#shadowbox").css("display", "none");
})
// 对删除按钮的设置
var remsgs = document.querySelectorAll('.per_can');
var removesure;
var thisindex = 0;
console.log(remsgs.length);
for (var i = 0; i < remsgs.length; i++) {
    remsgs[i].setAttribute("index", i);
    remsgs[i].addEventListener('click', function () {
        var remsgs = document.querySelectorAll('.per_can');
        for (var i = 0; i < remsgs.length; i++) {
            remsgs[i].setAttribute("index", i);
        }
        thisindex = this.getAttribute('index');
        removesure = confirm("确定要删除该用户吗？");
        if (removesure == true) {
            // re_ed_num = this.getAttribute("index");
            console.log(thisindex);
            $("#usersmsg tr").eq(thisindex).remove();
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/users/del_users",
                async: false,
                data: {
                    _id: arrId[thisindex]
                },
                success: function (data) {
                    console.log(thisindex);
                    console.log(this);
                }

            })
        }
    })

}
$("#expt table tr").odd().css("background-color", "rgba(196, 184, 184, 0.781)");

//搜索框
var exp_word = document.getElementById('exp_fram');
$("#explore").click(function () {
    if ($("#exp_fram").val() != '') {
        $("#expshadowtrue").css("display", "block");
        $.ajax({
            type: "post",
            url: "http://118.195.129.130:3000/users/getInfoByKw_users",
            async: false,
            data: {
                kw: exp_word.value
            },
            success: function (data) {
                console.log(data);
                $("#exp_fram").val('');
                for (var i = 0; i < data.data.length; i++) {
                    var tr = document.createElement('tr');
                    tr.innerHTML = '<tr><td style="width: 200px">' + data.data[i].us + '</td><td style="width: 300px">' + data.data[i].phone + '</td><td style="width: 100px">' + data.data[i].age + '</td><td style="width: 100px">' + transSex(Number(data.data[i].sex)) + '</td></tr>';
                    document.querySelector('#expt table').appendChild(tr);
                }
            }
        })
    }
})
$("#expt #userremove").click(function () {
    for (var i = 1; i < $("#expt table tr").length; i++) {
        $("#expt table tr").eq(i).remove();
    }
    $("#expshadowtrue").css("display", "none");
})
function lisblock(num) {
    for (var i = 0; i < usersmsg_lis.length; i++) {
        // usersmsg_lis[i].style.display = 'none';
        $("#usersmsg li").eq(i).hide();
    }
    // usersmsg_lis[num].style.display = 'block';
    $("#usersmsg li").eq(num).show();
    $("#pages").html(num + 1 + "/" + usersmsg_lis.length);
}
var userpages = 0;
$("#pages").html(userpages + 1 + "/" + usersmsg_lis.length);
$("#toright").click(function () {
    if (userpages == usersmsg_lis.length - 1) {
        userpages = 0;
    } else {
        userpages++;
    }
    lisblock(userpages);
})
$("#toleft").click(function () {
    if (userpages == 0) {
        userpages = usersmsg_lis.length - 1;
    } else {
        userpages--;
    }
    lisblock(userpages);
})
$("#usefront").click(function () {
    if (userpages == 0) {
        alert("已经是首页了");
    }
    userpages = 0;
    lisblock(userpages);
})
$("#uselast").click(function () {
    if (userpages == usersmsg_lis.length - 1) {
        alert("已经是尾页了");
    }
    userpages = usersmsg_lis.length - 1;
    lisblock(userpages);
})
var arrfood = [];
$.ajax({
    type: "post",
    url: "http://118.195.129.130:3000/food/getInfoByPage",
    async: false,
    data: {
        page: 1,
        per_page: ''
    },
    success: function (data) {
        arrfood = data.data;
        console.log(data);

        console.log(data.data.length);
    },
    error: function (err) {
        console.log(err);
    }
})
console.log(arrfood);
function foodType(num) {
    if (num == 0) {
        return '面';
    } else if (num == 1) {
        return '米';
    } else if (num == 2) {
        return '饮食'
    } else {
        return '水果'
    }
}
function foodcMath(str) {
    if (str == '面') {
        return 0;
    } else if (str == '米') {
        return 1;
    } else if (str == '饮食') {
        return 2;
    } else {
        return '3'
    }
}
var foodbox = document.getElementById('foodbox');
var n = 0;
//生成行
for (var i = 0; i < Math.ceil(arrfood.length / 5); i++) {
    let li = document.createElement('li');
    li.setAttribute('index', i);
    foodbox.appendChild(li);
    var foodlis = foodbox.querySelectorAll('li');
    $(foodlis).hide();
    $(foodlis).eq(0).show();
    for (var j = 0; j < 5; j++) {
        var tr = document.createElement('tr');
        tr.setAttribute('index', n);
        tr.setAttribute('foodId', arrfood[n]._id);
        tr.innerHTML = '<td>' + arrfood[n].name + '</td><td>' + arrfood[n].price + '</td><td style="width: 200px;">' + arrfood[n].desc + '</td><td>' + foodType(arrfood[n].typeid) + '</td><td>' + arrfood[n].typename + '</td><td><button style="background-color:green;" class="eid_food"><i class="fa fa-pencil" style="font-size:18px"></i>编辑</button></td><td><button style="background-color:red;" class="remove_food"><i class="fa fa-trash" style="font-size:18px"></i>删除</button></td>';
        foodlis[i].appendChild(tr);
        n++;
        if (n == arrfood.length) {
            break;
        }
        $("#foodbox li tr").even().css('background-color', 'skyblue');
    }
}
var eid_food = document.querySelectorAll('.eid_food');
var remove_food = document.querySelectorAll('.remove_food');
var tr_id = '';
var tr_index = 0;
for (var i = 0; i < arrfood.length; i++) {
    eid_food[i].onclick = function () {
        tr_id = this.parentNode.parentNode.getAttribute('foodId');
        tr_index = this.parentNode.parentNode.getAttribute('index');
        $("#food_eid_shadow").css("display", "block");
       eid_dish.value = food_tr[tr_index].children[0].innerHTML ;
        eid_price.value = food_tr[tr_index].children[1].innerHTML;
        eid_desc.value = food_tr[tr_index].children[2].innerHTML;
        eid_type.value= food_tr[tr_index].children[3].innerHTML;
        eid_typeid.value = food_tr[tr_index].children[4].innerHTML;

    }
    remove_food[i].onclick = function () {
        tr_id = this.parentNode.parentNode.getAttribute('foodId');
        tr_index = this.parentNode.parentNode.getAttribute('index');
        var trs = document.querySelectorAll('#foodbox tr');
        for (var i = 0; i < trs.length; i++) {
            trs[i].setAttribute('index', i);
        }
        var flag = confirm("确认要删除");
        if (flag) {
            console.log(tr_index);
            // var trs = document.querySelectorAll('#foodbox tr');
            $("#foodbox tr").eq(tr_index).remove();
            // for(var i = 0;i < trs.length;i++){
            //     trs[i].setAttribute('index',i);
            // }
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/food/del",
                async: false,
                data: {
                    _id: tr_id
                },
                success: function (data) {
                    alert('删除成功！');
                },
                error: function (err) {
                    console.log(err);
                }
            })
        }
    }
}
var eid_dish = document.getElementById('eid_dish');
var eid_price = document.getElementById('eid_price');
var eid_desc = document.getElementById('eid_desc');
var eid_type = document.getElementById('eid_type');
var eid_typeid = document.getElementById('eid_typeid');
$("#foodchangebox button").eq(1).click(function () {
    $("#food_eid_shadow").css("display", "none");
})
var food_tr = document.querySelectorAll('#foodbox tr');
$("#foodchangebox button").eq(0).click(function () {
    console.log(tr_id);
    if ($("#eid_dish").val() != '') {
        var flag = confirm("是否确定要进行修改？");
        if (flag) {
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/food/update",
                async: false,
                data: {
                    name: eid_dish.value,
                    price: eid_price.value,
                    desc: eid_desc.value,
                    typename: eid_type.value,
                    typeid: foodcMath(eid_typeid.value),
                    _id: tr_id
                },
                success: function (data) {
                    console.log(data);
                    console.log(eid_dish.value);
                    food_tr[tr_index].children[0].innerHTML = eid_dish.value;
                    food_tr[tr_index].children[1].innerHTML = eid_price.value;
                    food_tr[tr_index].children[2].innerHTML = eid_desc.value;
                    food_tr[tr_index].children[3].innerHTML = eid_type.value;
                    food_tr[tr_index].children[4].innerHTML = eid_typeid.value;
                    alert("修改成功！");
                    $("#food_eid_shadow").css("display", "none");
                },
                error: function (err) {
                    console.log(err);
                }
            })
        } else {
            alert("修改失败！");
        }
    } else {
        alert('菜品名不能为空！');
    }
})
$("#foodchangebox button").eq(1).click(function () {
    $("#food_eid_shadow").css("display", "none");
    eid_dish.value = '';
    eid_price.value = '';
    eid_desc.value = '';
    eid_type.value = '';
})
$("#foodchangebox i").click(function () {
    $("#food_eid_shadow").css("display", "none");
    eid_dish.value = '';
    eid_price.value = '';
    eid_desc.value = '';
    eid_type.value = '';
})
// 搜索框

var foodword = document.querySelector('#foodsec input');
$("#exp_food").click(function () {
    if (foodword.value != '') {
        $("#expfoodbox").css("display", "block");
        $.ajax({
            type: "post",
            url: "http://118.195.129.130:3000/food/getInfoByKw",
            async: false,
            data: {
                kw: foodword.value
            },
            success: function (data) {
                console.log(data);
                foodword.value = '';
                for (var i = 0; i < data.data.length; i++) {
                    var tr = document.createElement('tr');
                    // tr.setAttribute('index',i);
                    tr.innerHTML = '<tr><td style="width: 150px">' + data.data[i].name + '</td><td style="width: 100px;">' + data.data[i].price + '</td><td style="width: 250px">' + data.data[i].desc + '</td><td style="width: 100px">' + data.data[i].typename + '</td><td style="width: 100px">' + foodType(data.data[i].typeid) + '</td></tr>';
                    document.querySelector('#expfood table').appendChild(tr);
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
    }
})
$("#fclose").click(function () {
    var trs = document.querySelectorAll("#expfood tr");
    console.log(trs.length);
    console.log($("#expfoodbox #expfood table tr").length);
    for (var i = 1; i < trs.length; i++) {
        $(trs[i]).remove();
        // 就可奇怪，不知道为啥下面的那个不管用

        // $("#expfoodbox table tr").eq(i).remove();
    }
    $("#expfoodbox").css("display", "none");
})
$("#addfood").click(function () {

    $("#add_food_shadow").css("display", "block");
})
var add_dish = document.getElementById('add_dish');
var add_price = document.getElementById('add_price');
var add_desc = document.getElementById('add_desc');
var add_type = document.getElementById('add_type');
var add_typeid = document.getElementById('add_typeid');
$("#add_food button").eq(0).click(function () {
    if (add_dish.value != '') {
        var flag;
        flag = confirm("确认添加该菜信息吗？");
        if (flag) {
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/food/add",
                async: false,
                data: {
                    name: add_dish.value,
                    price: add_price.value,
                    desc: add_desc.value,
                    typename: add_type.value,
                    typeid: foodcMath(add_typeid.value)
                },
                success: function (data) {
                    console.log(data);
                    $("#add_food_shadow").css("display", "none");
                }
            })
        }
    } else {
        alert("菜名不能为空！");
    }
})
$("#add_food_shadow button").eq(1).click(function () {
    $("#add_food_shadow").css("display", "none");
    add_dish.value = '';
    add_price.value = '';
    add_desc.value = '';
    add_type.value = '';
    add_typeid.value = '';
})
$("#add_food_shadow i").click(function () {
    $("#add_food_shadow").css("display", "none");
    add_dish.value = '';
    add_price.value = '';
    add_desc.value = '';
    add_type.value = '';
    add_typeid.value = '';
})
//写一个箭头函数
var food = document.getElementById('food');
$("#food").html(1 + "/" + $("#foodbox li").length);
function targetPage(num) {
    for (var i = 0; i < $("#foodbox li").length; i++) {
        $("#foodbox li").eq(i).hide();
    }
    $("#foodbox li").eq(num).show();
    food.innerHTML = num + 1 + "/" + $("#foodbox li").length;
    // $("#food").html(num+1+"/ "+ $("#foodbox li").length);
}

var step = 0;
$("#arw-right").click(function () {
    if (step == $("#foodbox li").length - 1) {
        step = 0;
        targetPage(step);
    } else {
         step++;
    targetPage(step);
    }
   
})
$("#arw-left").click(function () {
    if (step == 0) {
        step = $("#foodbox li").length - 1;
        targetPage(step);
    } else {
         step--;
    targetPage(step);
    }
   
})
$("#foodfront").click(function () {
    if (step != 0) {
        step = 0;
        targetPage(step);
    } else {
        alert("已经是首页了！");
    }
})
$("#foodlast").click(function () {
    if (step != $("#foodbox li").length - 1) {
        step = $("#foodbox li").length - 1;
        targetPage(step);
    } else {
        alert("已经是尾页了！");
    }
})
//获取订单信息和数量
var orderarr = [];
$.ajax({
    type: "post",
    url: "http://118.195.129.130:3000/order/getInfoByPage_order",
    async: false,
    data: {
        page: 1,
        per_page: ''
    },
    success: function (data) {
        orderarr = data.data;
        console.log(data.data.length);

    },
    error: function (err) {
        console.log(err);
    }
})
console.log(orderarr);
function payC(num) {
    if (num == 0) {
        return "未支付";
    }
    return "已支付";
}
function payM(str) {
    if (str == "未支付" || str == '') {
        return 0;
    }
    return 1;
}
var orderbox = document.getElementById('orderbox');
for (var i = 0; i < Math.ceil(orderarr.length / 5); i++) {
    var li = document.createElement('li');
    orderbox.appendChild(li);
}
for (var i = 0; i < Math.ceil(orderarr.length / 5); i++) {
    $("#orderbox li").eq(i).hide();
}
$("#orderbox li").eq(0).show();
var n = 0;
var orderlis = orderbox.querySelectorAll("li");
console.log(orderlis);
for (let i = 0; i < Math.ceil(orderarr.length / 5); i++) {
    for (let j = 0; j < 5; j++) {
        var tr = document.createElement('tr');
        tr.setAttribute("indexid", orderarr[n]._id);
        tr.innerHTML = '<td style="width: 150px;">' + orderarr[n].us + '</td><td>' + orderarr[n].amount + '</td><td style="width: 250px;">' + orderarr[n].phone + '</td><td>' + payC(orderarr[n].pay) + '</td><td><button style="background-color:green;" class="eid_order"><i class="fa fa-pencil" style="font-size:18px"></i>编辑</button></td><td><button style="background-color:red;" class="remove_order"><i class="fa fa-trash" style="font-size:18px"></i>删除</button></td>';
        n++;
        orderlis[i].appendChild(tr);
        if (n == orderarr.length) {
            break;
        }
    }
}
$("#orderbox tr").even().css("background-color", "skyblue");
//编辑按钮
console.log($(".eid_order").length);

for (let i = 0; i < $(".eid_order").length; i++) {
    $(".eid_order").eq(i).click(function () {
        temp = this.parentNode.parentNode;
        $("#us").html("用户:" + temp.children[0].innerHTML);
        $("#order_eid_shadow").css("display", "block");
         eid_amount.value = temp.children[1].innerHTML;
        eid_pay.value = temp.children[3].innerHTML ;
    })
}
//取消编辑按钮和x
var eid_amount = document.getElementById('eid_amount');
var eid_pay = document.getElementById('eid_pay');

$("#orderchangebox button").eq(1).click(function () {
    $("#order_eid_shadow").css("display", "none");
    eid_amount.value = '';
    eid_pay.value = '';

})
$("#order_eid_shadow i").click(function () {
    $("#order_eid_shadow").css("display", "none");
    eid_amount.value = '';
    eid_pay.value = '';
})
$("#order_eid_shadow button").eq(0).click(function () {
    var flag = confirm("确定要修改订单信息吗？");
    console.log(temp);
    if (flag) {
        $.ajax({
            type: "post",
            url: "http://118.195.129.130:3000/order/update_order",
            async: false,
            data: {
                us: temp.children[0].innerHTML,
                amount: eid_amount.value,
                phone: temp.children[2].innerHTML,
                pay: payM(eid_pay.value),
                _id: temp.getAttribute('indexid')
            },
            success: function (data) {
                console.log(data);
                console.log(temp.getAttribute('indexid'));
                alert("修改成功！");
                temp.children[1].innerHTML = eid_amount.value;
                temp.children[3].innerHTML = eid_pay.value;
                $("#order_eid_shadow").css("display", "none");
                eid_amount.value = '';
                eid_pay.value = '';
            },
            error: function (err) {
                console.log(err);
            }
        })
    }
})
//删除按钮

var re_index;
for (var i = 0; i < $(".remove_order").length; i++) {
    $(".remove_order").eq(i).attr('index', i);
    $(".remove_order").eq(i).click(function () {
        var trs = document.querySelectorAll('#orderbox tr');
        for (let i = 0; i < $(".remove_order").length; i++) {
            $(".remove_order").eq(i).attr('index', i);
        }
        re_index = this.getAttribute('index');
        var flag = confirm("确认删除订单吗？");
        if (flag) {
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/order/del_order",
                async: false,
                data: {
                    _id: this.parentNode.parentNode.getAttribute('indexid')
                },
                success: function (data) {
                    console.log(data);

                    $("#orderbox tr").eq(re_index).remove();
                    //    $(".remove_order").eq(re_index).parent.parent.remove();
                },
                error: function (err) {
                    console.log(err);
                }

            })
        }
    })
}
//订单搜索框部分
var orderword = document.querySelector('#ordersec input');
$("#exp_order").click(function () {
    if (orderword.value != '') {
        $("#exporderbox").css("display", "block");
        $.ajax({
            type: "post",
            url: "http://118.195.129.130:3000/order/getInfoByKw_order",
            async: false,
            data: {
                kw: orderword.value
            },
            success: function (data) {
                // console.log(data);
                orderword.value = '';
                for (var i = 0; i < data.data.length; i++) {
                    var tr = document.createElement('tr');
                    // tr.setAttribute('index',i);
                    tr.innerHTML = '<tr><td style="width: 150px">' + data.data[i].us + '</td><td style="width: 100px;">' + data.data[i].amount + '</td><td style="width: 250px">' + data.data[i].phone + '</td><td style="width: 100px">' + payC(data.data[i].pay) + '</td></tr>';
                    document.querySelector('#exporder table').appendChild(tr);
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
    }
})
$("#oclose").click(function () {
    var trs = document.querySelectorAll("#exporder tr");
    console.log(trs.length);
    console.log($("#exporderbox #exporder table tr").length);
    for (var i = 1; i < trs.length; i++) {
        $(trs[i]).remove();
        // 就可奇怪，不知道为啥下面的那个不管用

        // $("#expfoodbox table tr").eq(i).remove();
    }
    $("#exporderbox").css("display", "none");
})
//添加订单
var add_us = document.getElementById('add_us');
var add_amount = document.getElementById('add_amount');
var add_phone = document.getElementById('add_phone');
var add_pay = document.getElementById('add_pay');
// var add_typeid = document.getElementById('add_typeid');
$("#addorder").click(function () {
    $("#add_order_shadow").css("display", "block");
})
$("#add_order button").eq(0).click(function () {
    if (add_us.value != '') {
        var flag;
        flag = confirm("确认添加该订单信息吗？");
        if (flag) {
            $.ajax({
                type: "post",
                url: "http://118.195.129.130:3000/order/add_order",
                async: false,
                data: {
                    us: add_us.value,
                    amount: add_amount.value,
                    phone: add_phone.value,
                    pay: payM(add_pay.value)
                },
                success: function (data) {
                    console.log(data.msg);
                    if (data.msg == "参数错误") {
                        alert("添加失败");
                    } else if (data.msg == "用户不存在") {
                        alert("用户不存在");
                    }
                    $("#add_order_shadow").css("display", "none");
                }
            })
        }
    } else {
        alert("用户名不能为空！");
    }
})
$("#add_order_shadow button").eq(1).click(function () {
    $("#add_order_shadow").css("display", "none");
    add_us.value = '';
    add_amount.value = '';
    add_phone.value = '';
    add_pay.value = '';

})
$("#add_order_shadow i").click(function () {
    $("#add_order_shadow").css("display", "none");
    add_us.value = '';
    add_amount.value = '';
    add_phone.value = '';
    add_pay.value = '';
})
//换页
$("#order").html(1 + "/" + $("#orderbox li").length);
function orderCpage(num) {
    for (var i = 0; i < $("#orderbox li").length; i++) {
        $("#orderbox li").eq(i).hide();
    }
    $("#orderbox li").eq(num).show();
    $("#order").html(num + 1 + "/" + $("#orderbox li").length);
}
var orderpage = 0;
$("#arw_right").click(function () {
    if (orderpage == $("#orderbox li").length - 1) {
        orderpage = 0;
        orderCpage(orderpage);
    } else {
        orderpage++;
        orderCpage(orderpage);
    }
})
$("#arw_left").click(function () {
    if (orderpage == 0) {
        orderpage = $("#orderbox li").length - 1;
        orderCpage(orderpage);
    } else {
        orderpage--;
        orderCpage(orderpage);
    }
})
$("#orderfront").click(function () {
    if (orderpage == 0) {
        alert("已近到达订单首页了！");
    } else {
        orderpage = 0;
        orderCpage(orderpage);
    }

})
$("#orderlast").click(function () {
    if (orderpage == $("#orderbox li").length - 1) {
        alert("已近到达订单尾页了！");
    } else {
        orderpage = $("#orderbox li").length - 1;
        orderCpage(orderpage);
    }
})
// }